package com.example.administrator.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.example.administrator.demo.Bean.news;

/**
 * Created by Administrator on 2016/4/17.
 */
public class News extends Fragment{

    //news视图，用来绑定各个控件的ID
    private View news;

    //下拉刷新
    private SwipeRefreshLayout swipeRefreshLayout = null;

    //ListView
    private ListView listView;

    //生成动态数组，用来加载数据
    private ArrayList<HashMap<String, String>> news_list = new ArrayList<HashMap<String, String>>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        //初始化news视图
        news = inflater.inflate(R.layout.news, container, false);
        //绑定下拉刷新控件的ID
        swipeRefreshLayout = (SwipeRefreshLayout) news.findViewById(R.id.swipe_news);
        //绑定XML中的ListView，作为Item的容器
        listView = (ListView) news.findViewById(R.id.listView);

        //下拉刷新的监听器
        swipeRefreshLayout.setOnRefreshListener(listener);
        listener.onRefresh();


        //设定ListView的点击触发事件
        listView = (ListView)news.findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(news.getContext(), detail.class);

                //获取选中项的HashMap对象
                HashMap<String, String> map = (HashMap<String,String>)listView.getItemAtPosition(position);
                intent.putExtra("ID", map.get("Item_ID"));

                startActivity(intent);
            }
        });

        return news;
    }

    SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            //先从ListView中删除之前的数据
            listView.setAdapter(null);

            add_data();
            swipeRefreshLayout.setRefreshing(false);
        }
    };


    //往界面里面添加数据
    private void add_data(){
        BmobQuery<news> query = new BmobQuery<news>();

        //查询条件
        query.addWhereContains("time", getDate());
        //限制查询结果条数为50条，默认为10条
        query.setLimit(50);
        //按时间降序
        query.order("-time");
        //执行查询
        query.findObjects(news.getContext(), new FindListener<news>() {
            @Override
            public void onSuccess(List<news> list) {
                for(news item : list){
                    //向动态数组里面加载查询到的数据
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("Item_ID", item.getID());
                    map.put("Item_title", item.getTitle());
                    map.put("Item_source", convert_source(item.getSource()));
                    map.put("Item_time", convert_time(item.getTime()));
                    map.put("Item_ID", item.getID());
                    news_list.add(map);
                }

                //生成适配器，数组==》ListItem
                SimpleAdapter schedule = new SimpleAdapter(news.getContext(), //当前上下文
                        news_list,  //数据来源
                        R.layout.list_item,  //list_item的xml实现
                        new String[] {"Item_title", "Item_source" , "Item_time", "Item_ID"},  //动态数组与list_item对应的子项
                        new int[] {R.id.Item_title, R.id.Item_source, R.id.Item_time, R.id.Item_ID});  //list_item的xml文件中4个TextView ID

                //添加并显示
                listView.setAdapter(schedule);
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    //转换来源信息
    private String convert_source(String source){
        String result = null;
        if(source.equals("sina")){
            result = "新浪";
        }
        else if (source.equals("tencent")){
            result = "腾讯";
        }
        else if (source.equals("netease")){
            result = "网易";
        }
        return result;
    }

    //转换时间信息，将12位的时间转化为mm-dd hh:mm
    private String convert_time(String time){
        String result;
        String month, day, hour, minute;
        month = time.substring(4,6);
        day = time.substring(6,8);
        hour = time.substring(8,10);
        minute = time.substring(10,12);

        result = month + "-" + day + " " + hour + ":" + minute;

        return result;
    }

    //获取今日日期
    private String getDate() {
        String time;
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        time = simpleDateFormat.format(date);

        return time;
    }


}
